
import { reqAddressadd } from "../../api/addressadd";
import { toast } from "../../utils/extendApi";

Page({
  data: {
    consignee:'',
    phone:'',
    state:'',
    city:'',
    district:'',
    streetAddress:'',
    isDefault:''

  },

  onAddressChange(event){
    // console.log(event);
    const [state,city,district]= event.detail.value;
    this.setData({
      state,
      city,
      district
    })
  },

  async addAddress(){
    const param = {
      consignee:this.data.consignee,
      phone:this.data.phone,
      state:this.data.state,
      city:this.data.city,
      district:this.data.district,
      streetAddress:this.data.streetAddress,
      default:this.data.isDefault,
      isPickup:false,
    }
    const {code} = await reqAddressadd(param)
    if (code==1){
    wx.navigateBack()
    wx.toast({title:'新增地址成功',icon:'success'})
  }else{
    wx.toast({title:'新增地址失败',icon:'error'})
  }


  },

  isdefaultchange() {
    // 获取当前 isDefault 的值
    const currentIsDefault = this.data.isDefault;
    
    // 根据当前值设置 instance 的新值
    let instance = !currentIsDefault;
    //  console.log(instance);
    
    // 如果需要更新 data 中的 isDefault 值，可以这样做
    this.setData({
      isDefault: instance
    });
  }
  
  
});